package com.magic.smartcity.view.more.hospital

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.magic.smartcity.R
import com.magic.smartcity.beans.HospitalBanner
import com.magic.smartcity.databinding.FragmentHospitalBinding
import com.magic.smartcity.view.more.adapters.HospitalBannerAdapter
import com.magic.smartcity.view.more.adapters.HospitalHomeListAdapter
import com.magic.smartcity.view.more.viewmodels.HospitalViewModel

class HospitalFragment : Fragment() {

    private val hospitalViewModel by lazy {
        ViewModelProvider(this).get(HospitalViewModel::class.java)
    }

    private lateinit var binding: FragmentHospitalBinding

    private var bannerAdapterList: MutableList<HospitalBanner> = mutableListOf()

    private val bannerAdapter = HospitalBannerAdapter(bannerAdapterList)
    private val hospitalListAdapter = HospitalHomeListAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        hospitalViewModel.getHospitalBanner(1)
        hospitalViewModel.getHospitalList()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHospitalBinding.inflate(layoutInflater)
        initView()

        return binding.root
    }

    private fun initView() {
        //设置轮播图数据
        binding.banner2.setAdapter(bannerAdapter)
        hospitalViewModel.hospitalBannerList.observe(viewLifecycleOwner) {
            bannerAdapter.setDatas(it)
        }

        //设置首页医院列表数据
        binding.hospitalRy.adapter = hospitalListAdapter
        binding.hospitalRy.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        hospitalViewModel.hospitalList.observe(viewLifecycleOwner) {
            hospitalListAdapter.setData(it)
        }

    }

}